package com.settlement.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.WebHook;
import com.settlement.system.model.form.WebHookForm;
import com.settlement.system.model.query.WebHookPageQuery;
import com.settlement.system.service.WebHookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * WEB_HOOK表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-10-23
 */
@RestController
@RequestMapping("/api/v1/webHook")
@RequiredArgsConstructor
public class WebHookController {

    private final WebHookService webHookService;

    @Operation(summary = "WEB_HOOK分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<WebHook> getPage(@ParameterObject WebHookPageQuery queryParams) {
        Page<WebHook> result = webHookService.getPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增或修改钩子", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("saveOrUpdate")
    @PreAuthorize("@ss.hasPerm('sys:webHook:save')")
    @PreventDuplicateSubmit
    public Result<?> saveOrUpdate(@Valid @RequestBody WebHookForm webHookForm) {
        webHookService.createOrUpdate(webHookForm);
        return Result.success();
    }

    @Operation(summary = "启用钩子", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("enable/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:webHook:status')")
    @PreventDuplicateSubmit
    public Result<?> enable(@Parameter(description = "钩子ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        webHookService.enable(ids);
        return Result.success();
    }

    @Operation(summary = "禁用钩子", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("disable/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:webHook:status')")
    @PreventDuplicateSubmit
    public Result<?> disable(@Parameter(description = "钩子ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        webHookService.disable(ids);
        return Result.success();
    }

    @Operation(summary = "删除钩子", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:webHook:delete')")
    public Result<?> deleteWebHooks(@Parameter(description = "钩子ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = webHookService.deleteWebHooks(ids);
        return Result.judge(result);
    }

    @Operation(summary = "WEB_HOOK表单数据", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{webHookId}/form")
    public Result<WebHook> getPlatformForm(
            @Parameter(description = "ID") @PathVariable Long webHookId
    ) {
        WebHook webHook = webHookService.getWebHookFormData(webHookId);
        return Result.success(webHook);
    }
}
